﻿using HZYProject.Core.Repositories;
using HZYProject.Domain;
using HZYProject.Web.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HZYProject.Web.Controllers
{
    [Route("mcategory")]
    public class MCategoryController : Controller
    {

        private readonly IRepository<ModuleCategory, int> _repository;

        public MCategoryController(IRepository<ModuleCategory, int> repository)
        {
            _repository = repository;
        }

        [HttpGet("")]
        public IActionResult Index()
        {
            return View();
        }
        [HttpPost("")]
        public IActionResult Index(MCategoryModel model)
        {
            var data = _repository.GetAllList();
            return Json(new { code = 0, msg = "", data = data, count = data.Count });
        }


        [HttpGet("add")]
        public IActionResult Add()
        {
            return View();
        }

        [HttpPost("add")]
        public IActionResult Add(MCAddAndUpdateModel model)
        {
            _repository.Insert(new ModuleCategory
            {
                CategoryName = model.CategoryName,
                Description = model.Description
            });

            return Json(new { success = 1, msg = "添加成功" });
        }

        [HttpGet("edit/{id}")]
        public IActionResult Edit(int id)
        {
            var r = _repository.FirstOrDefault(x => x.ModuleCategoryID == id);
            return View(r);
        }

        [HttpPost("edit")]
        public IActionResult edit(MCAddAndUpdateModel model)
        {
            var r = _repository.FirstOrDefault(x => x.ModuleCategoryID == model.ModuleCategoryID);

            r.CategoryName = model.CategoryName;
            r.Description = model.Description;

            _repository.Update(r);

            return Json(new { success = 1, msg = "修改成功" });
        }


        [HttpPost("del/{id}")]
        public IActionResult Del(int id)
        {

            ModuleCategory r = new ModuleCategory { ModuleCategoryID = id };
            _repository.Delete(r);
            return Json(new { success = 1, msg = "删除成功" });
        }

    }
}
